from flaskapp.util.Sqlalchemy import db
from datetime import date

class Rent(db.Model):
    __tablename__ = 'rent'

    rent_id = db.Column(db.Integer, primary_key=True)
    tenant_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    landlord_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    house_id = db.Column(db.Integer, db.ForeignKey('house.house_id'), nullable=False)
    start_date = db.Column(db.Date, nullable=False)
    end_date = db.Column(db.Date, nullable=False)
    amount = db.Column(db.Numeric(10, 2), nullable=False)
    rent_date = db.Column(db.Date, nullable=False)
    pay_type = db.Column(db.String(255), nullable=False)

    def __init__(self, tenant_id, landlord_id, house_id, start_date, end_date, amount, rent_date=None, pay_type='支付宝'):
        self.tenant_id = tenant_id
        self.landlord_id = landlord_id
        self.house_id = house_id
        self.start_date = start_date
        self.end_date = end_date
        self.amount = amount
        self.rent_date = rent_date if rent_date else date.today()
        self.pay_type = pay_type

    def to_dict(self):
        return {
            'rent_id': self.rent_id,
            'tenant_id': self.tenant_id,
            'landlord_id': self.landlord_id,
            'house_id': self.house_id,
            'start_date': self.start_date.isoformat() if self.start_date else None,
            'end_date': self.end_date.isoformat() if self.end_date else None,
            'amount': float(self.amount),
            'rent_date': self.rent_date.isoformat() if self.rent_date else None,
            'pay_type': self.pay_type
        }